<template>
  <div class="crm-page home-page">
    <div class="page-header">任务</div>
    <div class="page-content">
      <el-row class="table-btns" :gutter="20">
        <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="8">
          <el-select v-model="queryParam.status" style="width:200px" @change="changeStatus">
            <el-option label="今日任务" value="1" />
            <el-option label="未处理任务" value="2" />
            <el-option label="最近查看" value="3" />
            <el-option label="最近已完成任务" value="4" />
          </el-select>
        </el-col>
        <el-col class="align-right" :xs="12" :sm="12" :md="12" :lg="16" :xl="16">
          <el-button size="small" @click="handleAdd">新建</el-button>
        </el-col>
      </el-row>
      <BaseTable
        ref="table"
        :load-data="loadData"
        show-selection
      >
        <el-table-column
          label="操作"

          fixed="left"
          width="200"
          class-name="table-opt-col"
        >
          <template slot-scope="{row}">
            <el-button type="text" @click="handleUpdate(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
            <el-button type="text" @click="gotoEvent(row)">跟进</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="主题"

          prop="theme"
        >
          <template slot-scope="{row}">
            <a class="table-link" @click="gotoDetail(row)">{{ row.theme }}</a>
          </template>
        </el-table-column>
        <el-table-column
          label="名称"

          prop="name"
        >
          <template slot-scope="{row}">
            <a class="table-link" @click="gotoCustomerDetail(row)">{{ row.name }}</a>
          </template>
        </el-table-column>
        <el-table-column
          label="相关项"

          prop="relatedItems"
        />
        <el-table-column
          label="到期时间"

          prop="endDate"
        />
        <el-table-column
          label="所有人"

          prop="assigner"
        />
        <el-table-column
          label="创建日期"

          prop="createTime"
          width="200"
        >
          <template slot-scope="{row}">
            {{ formatTime(row.createTime) }}
          </template>
        </el-table-column>
      </BaseTable>
    </div>
    <AddDialog v-if="visible" :visible="visible" :data="curData" @close="closeDialog" />
    <Transfer :visible="visibleTransfer" :data="curData" @close="closeDialog" />
  </div>
</template>
<script>
import AddDialog from './add.vue'
import Transfer from './transfer'
import { getTaskList, deleteTask } from '@/api/task'
import { getEnumText } from '@/utils/enums'
import { formatTime } from '@/utils/tools'
export default {
  name: 'Contacts',
  components: { AddDialog, Transfer },
  data() {
    return {
      visible: false,
      visibleTransfer: false,
      curData: {},
      queryParam: {
        status: '1'

      }
    }
  },
  methods: {
    getEnumText,
    formatTime,
    loadData(params) {
      const queryParam = {
        searchType: this.queryParam.status
      }
      return getTaskList(Object.assign(params, queryParam)).then((res) => {
        // console.log('rrrrrr:', res)
        return res
      })
    },
    closeDialog(v) {
      this.visible = false
      this.visibleTransfer = false
      if (v) {
        this.loadData({})
      }
    },
    // 下拉状态查询
    changeStatus() {
      this.$refs.table.refresh(true)
    },
    handleAdd() {
      this.curData = {}
      this.visible = true
    },
    // 编辑
    handleUpdate(row) {

    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确定要删除此任务？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTask({ id: row.id }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.$refs.table.refresh()
        }).catch((error) => {
          console.log(error)
          this.$message({
            type: 'error',
            message: error.message || '删除失败'
          })
        })
      }).catch(() => {

      })
    },
    // 跟进
    gotoEvent(row) {
      this.$router.push({ path: '/event/info', query: { id: row.id }})
    }
  }
}
</script>
